package com.rchat.pocmini.media;

import android.content.Context;
import android.view.SurfaceHolder;

import com.airtalkee.sdk.AirtalkeeMediaVideoControl;
import com.airtalkee.sdk.OnMediaVideoListener;
import com.airtalkee.sdk.OnMediaVideoPictureListener;

/**
 * Created by ljd on 2017/2/6.
 * <p>
 * 接管视频的实时传输控制
 * <p>
 * OnMediaVideoListener,在开始录制视频或停止时调用，关心此类事件的实例需事先次Interface才会被调用
 * OnMediaVideoPictureListener , 视频传输时，远端进行抓拍的监听，关心此类事件的实例需事先此Interface才会被调用
 */

public class PocMediaVideoControl implements PocMediaVideoControlInterface {

    private static AirtalkeeMediaVideoControl airtalkeeMediaVideoControl = AirtalkeeMediaVideoControl.getInstance();
    private static PocMediaVideoControl pocMediaVideoControl;

    //========= 摄像头方向值 ==========//
    //纵向
    public static final int CAMMERA_SCREEN_ORIENTATION_PORTRAIT = AirtalkeeMediaVideoControl.CAMERA_SCREEN_ORIENTATION_PORTRAIT;
    //向右翻转90°
    public static final int CAMERA_SCREEN_ORIENTATION_LANDSCAPE_RIGHT = AirtalkeeMediaVideoControl.CAMERA_SCREEN_ORIENTATION_LANDSCAPE_RIGHT;
    //向左翻转90°
    public static final int CAMETA_SCREEN_ORIENTATION_LANDSCAPE_LEFT = AirtalkeeMediaVideoControl.CAMERA_SCREEN_ORIENTATION_LANDSCAPE_LEFT;

    private PocMediaVideoControl() {
    }

    /**
     * 获取AirtalkeeMediaVideoControl实例
     *
     * @return airtalkeeMediaVideoControl
     */
    public static PocMediaVideoControl getPocMediaVideoControlInstance() {
        if (pocMediaVideoControl == null) {
            pocMediaVideoControl = new PocMediaVideoControl();
        }
        return pocMediaVideoControl;
    }

    /**
     * 设置监听器，关心OnMediaVideoListener事件的实例，均需要通过此接口注册，否则将接受不到相应事件
     *
     * @param listener 监听器
     */
    @Override
    public void setOnVideoListener(OnMediaVideoListener listener) {
        airtalkeeMediaVideoControl.setOnVideoListener(listener);
    }

    /**
     * 设置监听器，关心OnMediaVideoListener事件的实例，均需要通过此接口注册，否则将收不到相应事件
     *
     * @param listener 监听器
     */
    @Override
    public void setOnMediaVideoPictureListener(OnMediaVideoPictureListener listener) {
        airtalkeeMediaVideoControl.setOnMediaVideoPictureListener(listener);
    }

    /**
     * 初始化（开启）系统相机
     *
     * @param context           Activity上下文
     * @param surfaceHolder     Surface监听器（实现SurfaceHolder接口）
     * @param cameraWidth       相机宽度
     * @param cameraHeight      相机高度
     * @param cameraOrientation 相机方向
     * @param cameraBitRate     相机比特率
     * @param cameraFrameRate   相机帧率
     */
    @Override
    public void videoActionCameraStart(Context context, SurfaceHolder surfaceHolder, int cameraWidth, int cameraHeight, int cameraOrientation, int cameraBitRate, int cameraFrameRate) {
        airtalkeeMediaVideoControl.VideoActionCameraStart(context, surfaceHolder, cameraWidth, cameraHeight, cameraOrientation, cameraBitRate, cameraFrameRate);
    }

    /**
     * 重置视频录制相机（关闭并释放系统相机）
     */
    @Override
    public void videoActionCameraStop() {
        airtalkeeMediaVideoControl.VideoActionCameraStop();
    }

    /**
     * 设置相机方向监督
     *
     * @param context           Activity上下文
     * @param cameraOrientation 相机角度
     */
    @Override
    public void videoActionCameraOrientation(Context context, int cameraOrientation) {
        airtalkeeMediaVideoControl.VideoActionCameraOrientation(context, cameraOrientation);
    }

    /**
     * 视频录制开始
     *
     * @param sessionCode Session会话code
     * @return 返回video标示，用于停止
     */
    @Override
    public int videoActionRecorderStart(String sessionCode) {
        return airtalkeeMediaVideoControl.VideoActionRecorderStart(sessionCode);
    }

    /**
     * 结束录制视频，ResVideoStreamRecorderListener.onVideoRecorderStop
     *
     * @param videoId 开始录制时产生的相应video标识
     */
    @Override
    public void videoActionRecorderStop(int videoId) {
        airtalkeeMediaVideoControl.VideoActionRecorderStop(videoId);
    }


    /**
     * 设置传输比特率
     *
     * @return 传输比特路
     */
    @Override
    public int getVideoRealtimeBitRate() {
        return airtalkeeMediaVideoControl.VideoGetRealtimeBitrate();
    }

    /**
     * 设置闪光模式
     *
     * @param isOn 开关
     * @return 闪光灯状态，true开，false关
     */
    @Override
    public boolean videoCameraFlash(boolean isOn) {
        return airtalkeeMediaVideoControl.VideoCameraFlash(isOn);
    }

    /**
     * 获取闪光模式
     *
     * @return 闪光灯状态，true开，false关
     */
    @Override
    public boolean videoCameraFlashIsOn() {
        return airtalkeeMediaVideoControl.VideoCameraFlashIsOn();
    }

}
